﻿function onFieldDrop(e) {
    var $target = $(e.dropTarget);
    if ($target.is('ul.ws-fields')) {
        e.preventDefault();
        var nodetext = getTreeviewSelectedItemText($('#fields'), e);

        var $win = $('#Window').data('tWindow');
        var $newnode = $('.ws-fields').addListItem(null);
        var controltype = $(e.item).attr('controltype');
        initialFieldLi($newnode, controltype, nodetext);
    }
}

function initialFieldLi($newnode, controltype, nodetext,id) {
    var $target = $('.ws-fields');
    var width = $newnode.data('lidata') == null ? 1 : $newnode.data('lidata').FieldWidth;
    $newnode.css('width', $target.parent().width() * 0.95 * 0.25 * width).attr('controltype', controltype).val(nodetext).find('.text-display').text(nodetext).end();
    var $sliderWrapper = $('<div></div>').appendTo($newnode);

    var $widthslide = $('<div style="width:100px;float:left"><div>').appendTo($sliderWrapper).slider({
        value: 1,
        min: 1,
        max: 4,
        step: 1,
        slide: function (event, ui) {
            $newnode.css('width', $target.parent().width() * 0.95 * ui.value * 25 / 100);
            var $itemwidth = ui.value * 25 + '%';
            $display.html('宽度：' + $itemwidth);
            $newnode.attr('fieldwidth', ui.value);

            var data = $newnode.data('lidata');
            if (data != null) {
                data.FieldWidth = ui.value;
                $newnode.data('lidata', data);
            }
        }
    });

    $newnode.attr('fieldwidth', width);
    var $display = $('<div  style="width:30%;float:left;margin-top:-3px;margin-left:3px">宽度：25%</div>').appendTo($sliderWrapper);
    $newnode.addHoverFunctions({
        deleteEvent: function () {
            $newnode.remove();
        },
        detailEvent: function () {
            setDetailToWindowEditor($newnode, initialFieldDetail($newnode), $('#Window'));
        }
    });
}

function initialFieldDetail($target) {
    var data = $target.data('lidata');//数据值
    var array = new Array();
    var controltype = $target.attr('controltype');

    //以下次序定义不可更改
    if (controltype == 'text' || controltype == 'richtext') {
        array.push({ controltype: 'text', fieldname: '字段名称', fieldvalue: data == null ? null : data.FieldName, must: true });
        array.push({ controltype: 'text', fieldname: '默认字值', fieldvalue: data == null ? null : data.DefaultValue, must: false });
        array.push({ controltype: 'radio', fieldname: '是否必填', fieldvalue: data == null ? null : data.Must, optionalvalue: '必填||选填' });
        array.push({ controltype: 'slide', fieldname: '表单级别', fieldvalue: data == null ? 1 : data.Level, min: 1, max: 10, step: 1 });
    }
    else if (controltype == 'number' ) {
        array.push({ controltype: 'text', fieldname: '字段名称', fieldvalue: data == null ? null : data.FieldName, must: true });
        array.push({ controltype: 'number', fieldname: '默认字值', fieldvalue: data == null ? 0 : data.DefaultValue, must: false });
        array.push({ controltype: 'number', fieldname: '最小数值', fieldvalue: data == null ? null : data.Min });
        array.push({ controltype: 'number', fieldname: '最大数值', fieldvalue: data == null ? null : data.Max });
        array.push({ controltype: 'text', fieldname: '统计单位', fieldvalue: data == null ? null : data.Unit });
        array.push({ controltype: 'radio', fieldname: '是否必填', fieldvalue: data == null ? null : data.Must, optionalvalue: '必填||选填' });
        array.push({ controltype: 'slide', fieldname: '表单级别', fieldvalue: data == null ? 1 : data.Level, min: 1, max: 10, step: 1 });
    }
    else if (controltype == 'singleselection') {
        array.push({ controltype: 'text', fieldname: '字段名称', fieldvalue: data == null ? null : data.FieldName, must: true });
        array.push({ controltype: 'text', fieldname: '可选选项', fieldvalue: data == null ? null : data.OptionValue, must: true });
        array.push({ controltype: 'radio', fieldname: '是否必填', fieldvalue: data == null ? null : data.Must, optionalvalue: '选填||必填' });
        array.push({ controltype: 'radio', fieldname: '设初始值', fieldvalue: data == null ? null : data.Initial, optionalvalue: '第一项选中||全部为非选中' });
        array.push({ controltype: 'slide', fieldname: '表单级别', fieldvalue: data == null ? 1 : data.level, min: 1, max: 10, step: 1 });
    }
    else if (controltype == 'multipleselection') {
        array.push({ controltype: 'text', fieldname: '字段名称', fieldvalue: data == null ? null : data.FieldName, must: true });
        array.push({ controltype: 'text', fieldname: '可选选项', fieldvalue: data == null ? null : data.OptionValue, must: true });
        array.push({ controltype: 'radio', fieldname: '是否必填', fieldvalue: data == null ? null : data.Must, optionalvalue: '必填||选填' });
        array.push({ controltype: 'slide', fieldname: '表单级别', fieldvalue: data == null ? 1 : data.Level, min: 1, max: 10, step: 1 });
    }
    else if (controltype == 'slide') {
        array.push({ controltype: 'text', fieldname: '字段名称', fieldvalue: data == null ? null : data.FieldName });
        array.push({ controltype: 'text', fieldname: '当前数值', fieldvalue: data == null ? null : data.DefaultValue });
        array.push({ controltype: 'text', fieldname: '最小数值', fieldvalue: data == null ? null : data.Min });
        array.push({ controltype: 'text', fieldname: '最大数值', fieldvalue: data == null ? null : data.Max });
        array.push({ controltype: 'text', fieldname: '每步数值', fieldvalue: data == null ? null : data.Step });
        array.push({ controltype: 'text', fieldname: '统计单位', fieldvalue: data == null ? null : data.Unit });
        array.push({ controltype: 'slide', fieldname: '表单级别', fieldvalue: data == null ? 1 : data.Level, min: 1, max: 10, step: 1 });
    }
    else if (controltype == 'peoplefinder') {
        array.push({ controltype: 'text', fieldname: '字段名称', fieldvalue: data == null ? null : data.FieldName });
        array.push({ controltype: 'radio', fieldname: '是否必填', fieldvalue: data == null ? null : data.Must, optionalvalue: '必填||选填' });
        array.push({ controltype: 'slide', fieldname: '表单级别', fieldvalue: data == null ? 1 : data.Level, min: 1, max: 10, step: 1 });
    }
    else if (controltype == 'dropdownlist') {
        array.push({ controltype: 'text', fieldname: '字段名称', fieldvalue: data == null ? null : data.FieldName, must: true });
        array.push({ controltype: 'text', fieldname: '可选选项', fieldvalue: data == null ? null : data.OptionValue, must: true });
        array.push({ controltype: 'radio', fieldname: '是否必填', fieldvalue: data == null ? null : data.Must, optionalvalue: '必填||选填' });
        array.push({ controltype: 'slide', fieldname: '表单级别', fieldvalue: data == null ? 1 : data.Level, min: 1, max: 10, step: 1 });
    }
    else if (controltype == 'datepicker') {
        array.push({ controltype: 'text', fieldname: '字段名称', fieldvalue: data == null ? null : data.FieldName, must: true });
        array.push({ controltype: 'radio', fieldname: '是否必填', fieldvalue: data == null ? null : data.Must, optionalvalue: '必填||选填' });
        array.push({ controltype: 'slide', fieldname: '表单级别', fieldvalue: data == null ? 1 : data.Level, min: 1, max: 10, step: 1 });
    }
  
    $target.data('av', array);

    var win = $('#Window').data('tWindow');
    var $table = $target.getTable(
                         { save: function () {
                             if ($target.attr('sucess') == 'true') {
                                 $target.find('.text-display').text(array[0].fieldvalue);
                                 $target.removeClass('unsave').addClass('saved');
                                 $target.attr('uniquename', array[0].fieldvalue);
                                 saveFieldData($target);
                                 win.close();
                             }
                         },
                             cancel: function () {
                                 win.close();
                             }
                         });
    return $table;
}

function saveFieldData($li) {
    var data = $li.data('av');
        var id = $li.attr('id');
        var workflowid = $('#workflowid').val();
        var controltype = $li.attr('controltype');
        var fieldwidth = $li.attr('fieldwidth');
        var lidata;
        if (controltype == 'text' || controltype == 'richtext')
            lidata = { WorkflowID: workflowid, ID: id, FieldWidth: fieldwidth, ControlType: controltype, FieldName: data[0].fieldvalue, DefaultValue: data[1].fieldvalue, Must: data[2].fieldvalue, Level: data[3].fieldvalue };
        else if(controltype =='number')
            lidata = { WorkflowID: workflowid, ID: id, FieldWidth: fieldwidth, ControlType: controltype, FieldName: data[0].fieldvalue, DefaultValue: data[1].fieldvalue, Min: data[2].fieldvalue, Max: data[3].fieldvalue,Unit:data[4].fieldvalue, Must: data[5].fieldvalue, Level: data[6].fieldvalue };
        else if (controltype == 'singleselection')
            lidata ={ WorkflowID: workflowid, ID: id, FieldWidth: fieldwidth, ControlType: controltype, FieldName: data[0].fieldvalue, OptionValue: data[1].fieldvalue, Must: data[2].fieldvalue, Initial: data[3].fieldvalue, Level: data[4].fieldvalue };
        else if (controltype == 'multipleselection')
            lidata ={ WorkflowID: workflowid, ID: id, FieldWidth: fieldwidth, ControlType: controltype, FieldName: data[0].fieldvalue, OptionValue: data[1].fieldvalue, Must: data[2].fieldvalue, Level: data[3].fieldvalue };
        else if (controltype == 'slide')
            lidata = { WorkflowID: workflowid, ID: id, FieldWidth: fieldwidth, ControlType: controltype, FieldName: data[0].fieldvalue, CurrentValue: data[1].fieldvalue, Min: data[3].fieldvalue, Max: data[4].fieldvalue, Unit: data[4].fieldvalue, Level: data[3].fieldvalue };
        else if (controltype == 'peoplefinder')
            lidata ={ WorkflowID: workflowid, ID: id, FieldWidth: fieldwidth, ControlType: controltype, FieldName: data[0].fieldvalue, Must: data[1].fieldvalue, Level: data[2].fieldvalue };
        else if (controltype == 'dropdownlist')
            lidata ={ WorkflowID: workflowid, ID: id, FieldWidth: fieldwidth, ControlType: controltype, FieldName: data[0].fieldvalue, OptionValue: data[1].fieldvalue, Must: data[2].fieldvalue, Level: data[3].fieldvalue };
        else if (controltype == 'datepicker')
            lidata ={ WorkflowID: workflowid, ID: id, Fieldwidth: fieldwidth, ControlType: controltype, FieldName: data[0].fieldvalue, Must: data[1].fieldvalue, Level: data[2].fieldvalue };

        $li.data('lidata', lidata);
}

function getFieldsData() {
    var array = new Array();
    var i = 0;
    $('.ws-fields').children('li').each(function () {
        var $li = $(this);
        var data = $li.data('lidata');
        data.Sequence = i;
        i++;
        array.push(data);
    });
    return array;
}

function setFieldsData(data) {
    if(data.length>0) {
       var  $ul = $('.ws-fields');
        for (var i = 0; i < data.length; i++) {
            var $li = $ul.addListItem({ lidata: data[i] });
            var controltype = data[i].ControlType;
            var uniquename = data[i].FieldName;
            $li.attr('id', data[i].ID);
            $li.attr('uniquename', uniquename)
            initialFieldLi($li, controltype, uniquename);
            
        } 
    }
}
